
//
//  Created by 黄振 on 16/8/23.
//  Copyright © 2016年 黄振. All rights reserved.
//

#import "MPickerSelectionView.h"
#import "UIView+Layout.h"

#define RGBA(r, g, b, a)                    [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define ScreenWidth                         [[UIScreen mainScreen] bounds].size.width
#define ScreenHeight                        [[UIScreen mainScreen] bounds].size.height
@interface MPickerSelectionView ()

@property (nonatomic, strong, readwrite) UIButton *cancelButton;
@property (nonatomic, strong, readwrite) UIButton *confirmButton;
@property (nonatomic, strong) UIView *pickerBackgroundView;
@property (nonatomic, strong) UIButton *blackBackgroundButton;
@property (nonatomic, strong) UIView *returnView;

@end
@implementation MPickerSelectionView

- (UIButton *)blackBackgroundButton {
    if (!_blackBackgroundButton) {
        _blackBackgroundButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _blackBackgroundButton.backgroundColor = RGBA(0, 0, 0, 0.2);
        [_blackBackgroundButton addSubview:self.pickerBackgroundView];
        [_blackBackgroundButton addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];

    }
    return _blackBackgroundButton;
}

- (void)buttonClick {
    [self dismiss];
}

- (UIView *)pickerBackgroundView {
    if (!_pickerBackgroundView) {
        _pickerBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, 254)];
        _pickerBackgroundView.backgroundColor = RGBA(240,241,244,1);
        [_pickerBackgroundView addSubview:self.middleLabel];
        [_pickerBackgroundView addSubview:self.cancelButton];
        [_pickerBackgroundView addSubview:self.confirmButton];
        [_pickerBackgroundView addSubview:self.returnView];
    }
    return _pickerBackgroundView;
}

- (UIView *)returnView {
    if (!_returnView) {
        _returnView = [self viewWithMPickerSelectionView:self];
        _returnView.frame = CGRectMake(0, 44.0, ScreenWidth, 210);
    }
    return _returnView;
}

- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 90, 44)];
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:17.0];
        [_cancelButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:RGBA(43, 156, 237, 1) forState:UIControlStateNormal];
        [_cancelButton setTitleColor:RGBA(85, 85, 85, 1) forState:UIControlStateHighlighted];
        [_cancelButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 27, 0, 0)];
        [_cancelButton addTarget:self
                          action:@selector(cancelButtonEvent)
                forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

- (UILabel *)middleLabel {
    if (!_middleLabel) {
        _middleLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, ScreenWidth-180, 44)];
        _middleLabel.textAlignment = NSTextAlignmentCenter;
        _middleLabel.textColor = RGBA(119, 119, 119, 1);
        _middleLabel.font = [UIFont systemFontOfSize:17.0];
        _middleLabel.backgroundColor = [UIColor clearColor];
    }
    return _middleLabel;
}

- (UIButton *)confirmButton {
    if (!_confirmButton) {
        _confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-90, 0, 90, 44)];
        _confirmButton.titleLabel.font = [UIFont systemFontOfSize:17.0];
        [_confirmButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        [_confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:RGBA(43, 156, 237, 1) forState:UIControlStateNormal];
        [_confirmButton setTitleColor:RGBA(85, 85, 85, 1) forState:UIControlStateHighlighted];
        [_confirmButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 27)];
        [_confirmButton addTarget:self
                           action:@selector(confirmButtonEvent)
                 forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmButton;
}

- (void)present {
    [[UIApplication sharedApplication].keyWindow addSubview:self.blackBackgroundButton];
    self.isUnfold = YES;
    [UIView animateWithDuration:0.3 animations:^{
        self.pickerBackgroundView.top = ScreenHeight-254;
    }];
}

- (void)dismiss {
    self.isUnfold = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.pickerBackgroundView.top = ScreenHeight;
    } completion:^(BOOL finished) {
        [self.blackBackgroundButton removeFromSuperview];
    }];
}

- (void)cancelButtonEvent {
    if ([self.delegate respondsToSelector:@selector(cancelEventWithMPickerSelectionView:)]) {
        [self.delegate cancelEventWithMPickerSelectionView:self];
    }
    [self dismiss];
}

- (void)confirmButtonEvent {
    if ([self.delegate respondsToSelector:@selector(confirmEventWithMPickerSelectionView:)]) {
        [self.delegate confirmEventWithMPickerSelectionView:self];
    }
    [self dismiss];
}

- (UIView *)viewWithMPickerSelectionView:(MPickerSelectionView *)pickerSelectionView {
    return [self.delegate viewWithMPickerSelectionView:self];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
